package Data::Conveyor::Monitor;

# $Id: Monitor.pm 13653 2007-10-22 09:11:20Z gr $

use strict;
use warnings;


our $VERSION = '0.02';


use base 'Class::Scaffold::Storable';


sub sort_by_stage_order {
    my ($self, @activity) = @_;

    my %stage_order;
    my $order = 1;
    $stage_order{$_} = sprintf "%02d" => $order++ for
        map { "$_" }
        map {
            $self->delegate->make_obj('value_ticket_stage')->new_start($_),
            $self->delegate->make_obj('value_ticket_stage')->new_active($_),
            $self->delegate->make_obj('value_ticket_stage')->new_end($_),
        } $self->delegate->STAGE_ORDER;

    my @sorted =
        map  { $_->[0] }
        sort { $a->[1] cmp $b->[1] }
        map  { [ $_,
                 ($stage_order{$_->{stage}} || '00') . ($_->{status} || ' ')
               ] }
        @activity;

    wantarray ? @sorted : \@sorted;
}


sub sif_top {
    my ($self, %opt) = @_;

    my $result = $self->delegate->make_obj('service_result_container');

    my @activity = $opt{all}
        ? $self->storage->get_activity
        : $self->storage->get_activity_running;

    $result->result_push(
        $self->delegate->make_obj('service_result_tabular')->set_from_rows(
            fields => [ qw/count stage status rc oticket ochanged/ ],
            rows   => [ $self->sort_by_stage_order(@activity) ],
        )
    );

    $result->result_push(
        $self->delegate->make_obj('service_result_scalar', result =>
            sprintf("%d open regtransfers\n",
                $self->storage->count_open_regtransfers)
        )
    );

    $result;
}


1;


__END__

{% USE p = PodGenerated %}

=head1 NAME

{% p.package %} - stage-based conveyor-belt-like ticket handling system

=head1 SYNOPSIS

    {% p.package %}->new;

=head1 DESCRIPTION

None yet. This is an early release; fully functional, but undocumented. The
next release will have more documentation.

=head1 METHODS

=over 4

{% p.write_methods %}

=back

{% p.write_inheritance %}

{% PROCESS standard_pod %}

=cut

